package com.vma.shoeSystem.controller;

import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.vma.shoeSystem.util.ReturnsUtil;
import com.vma.shoes.bizModel.model.SpecItem;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chenzui on 2017/4/2.
 */
public class SpecController extends Controller{
    public void add(){
        try {
            String specName = getPara("specName");
            Integer groupSpecId = getParaToInt("groupSpecId");
            new SpecItem().setSpecGroupId(groupSpecId).setSpecValue(specName).save();
            renderJson(ReturnsUtil.returnSuccess("新增类别成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }

    public void update(){
        try {
            String specName = getPara("specName");
            Integer groupSpecId = getParaToInt("groupSpecId");
            Integer id = getParaToInt("id");
            SpecItem.dao.findById(id).setSpecValue(specName).setSpecGroupId(groupSpecId).update();
//            renderJson(HttpKit.post("http://app.kapull.com/lottery/gryLatestRecord","{\"test\":\"test\"}"));
            renderJson(ReturnsUtil.returnSuccess("更新"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }

    public void delete(){
        try {
            Integer id = getParaToInt("id");
            SpecItem specItem = SpecItem.dao.findById(id);
            specItem.delete();
//            renderJson(HttpKit.post("http://app.kapull.com/lottery/gryLatestRecord","{\"test\":\"test\"}"));
            renderJson(ReturnsUtil.returnSuccess("更新"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }

    public void qrySpecList(){
        try {
            String specName = getPara("specName");
            String groupId = getPara("groupId");

            int pageNo = getParaToInt("cPage");
            int pageSize = getParaToInt("pSize");

            List<String> param = new ArrayList<String>();
            String condition = "";


            if(specName != null && !"".equals(specName)){
                condition += "and si.spec_value like ? ";
                param.add("%"+specName+"%");
            }

            if(groupId != null && !"".equals(groupId)){
                condition += "and si.spec_group_id = ? ";
                param.add(groupId);
            }
            Page<Record> specItemPage = Db.paginate(pageNo,pageSize,"select si.*,sg.group_desc ","from spec_item si,spec_group sg where  1=1 "
                    +"and si.spec_group_id = sg.id "+condition+" order by si.spec_group_id"
                    ,param.toArray());
            renderJson(ReturnsUtil.returnSuccess(specItemPage));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }
}
